// pages/search/search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue: '',
        list: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.testKuoHao();
        this.testZhongkuo();
    },

    //匹配()
    testKuoHao() {
        var aa = "ldfjsldfj(123))AAA";
        //1 查找(开始
        //2 ([^)]*)中是匹配内容，不包含)的任意字符，零个或多个的字符
        var result = aa.match(/\(([^)]*)\)/);
        //下面这种方式更加好理解：
        // var result = aa.match(/\((.*)\)/);
        // 此时result＝["(123)", "123"];
        console.log(result);
        if (result) {
            console.log(result[1]); // "123"
        }
    },

    //匹配[]
    testZhongkuo() {
        var aa = "ldfjsldfj[123]AAA";
        //1 查找[开始
        //2 匹配多个字符
        //3 查找]结束
        var result = aa.match(/\[(.*)\]/);
        console.log(result);
        if (result) {
            console.log(result[1]); // "123"
        }
    },

    inputChange(e) {
        let self = this,
            value = e.detail.value;
        if (value == '') {
            var list = [];
            self.setData({
                list: list
            });
            return;
        }
        console.log(value);
        self.request(value);
    },

    request(value) {
        var self = this;
        var url = 'http://suggestion.baidu.com/su?wd=' + value;
        wx.request({
            url: url,
            success(res) {
                console.log(res.data);
                //window.baidu.sug({q:"1",p:false,s:["18,19","114电影网","126","131","1688","12123","12345","10月1日放假安排2019","188比分直播","118图库"]});
                //通过正则截取json字符串
                var result = res.data.match(/\(([^)]*)\)/);
                if (result) {
                    console.log(result[1]);
                    var data = result[1];
                    //获取[]的数组信息
                    result = data.match(/\[(.*)\]/);
                    console.log(result[0]);
                    //["114电影网","131","1688","12345","10月1日放假安排2019","188比分直播","118图库","139邮箱","126邮箱登录","123"]
                    var array = result[0];
                    console.log(array);
                    //字符串格式的json转换成json对象
                    array = JSON.parse(array);
                    // var array = ["114电影网", "131", "1688", "12345", "10月1日放假安排2019", "188比分直播", "118图库", "139邮箱", "126邮箱登录", "123"];
                    console.log(array);
                    var list = [];
                    list.push(...array);
                    self.setData({
                        list: list
                    });
                }
            },
        })
    },
})